package com.asset.salesforce.dao;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.model.Lead;


@Repository
public class LeadDaoImpl {
	@Autowired
	SessionFactory sessionFactory;
	@SuppressWarnings("unchecked")
	public List<Lead> getAllLead(Integer index,Integer companyId,Integer count) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Lead  where isDeleted= :isDeleted and isConverted=:isConverted and leadOwner.company.id = :companyId ");
		query.setParameter("isDeleted", false);
		query.setParameter("isConverted", false);
		query.setParameter("companyId", companyId);
		query.setFirstResult((index*count));
		query.setMaxResults(count);
		List<Lead> leads;
		leads = query.list();
		return  leads;
	}
	@SuppressWarnings("unchecked")
	public Integer getAllLeadSize(Integer companyId) {
		Session session =sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Lead where isDeleted= :isDeleted And leadOwner.company.id = :companyId");
		query.setParameter("isDeleted", false);
		query.setParameter("companyId", companyId);
		List<Lead> leads= new ArrayList<Lead>();
		leads = query.list();
		if(leads != null && leads.size() > 0){
		return  leads.size();
		}
		else{return null;}
	}

	public Lead addLead(Lead lead) {
		try{
		sessionFactory.getCurrentSession().save(lead);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return lead;
	}
	public List<Lead> getLead(Integer leadOwnerId){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Lead where  leadOwner.id = :leadOwnerId and isConverted=:isConverted and isDeleted=:isDeleted");
		query.setParameter("leadOwnerId", leadOwnerId);
		query.setParameter("isConverted", false);
		query.setParameter("isDeleted", false);
		@SuppressWarnings("unchecked")
		List<Lead>  leads= query.list();
		if(leads != null && leads.size() > 0)
		return leads;
		else
			return new ArrayList<Lead>();
		
	}
	
	public Lead getLeadByEmpId(Integer empId){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Lead where  leadOwner.id = :empId");
		query.setParameter("empId", empId);
		@SuppressWarnings("unchecked")
		List<Lead>  leads= query.list();
		if(leads.size() > 0)
		return leads.get(0);
		else
			return null;
		
	}
	
	public Lead getLeadById(Integer Id){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Lead where  id = :Id and isDeleted=:isDeleted");
		query.setParameter("Id", Id);
		query.setParameter("isDeleted", false);
		@SuppressWarnings("unchecked")
		List<Lead>  leads= query.list();
		if(leads.size() > 0)
		return leads.get(0);
		else
			return null;
		
	}

	public void saveOrupdate(Lead lead){
		Session session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(lead);
	}
	
	public Lead loadLead(Integer id) {
		Lead lead = (Lead)sessionFactory.getCurrentSession().get(Lead.class, id);
		return lead;
		
	}
	
	public void deleteLead(Lead lead) {
		sessionFactory.getCurrentSession().delete(lead);
		
	}	
	
	public List<String> getViewLeads(List<String> fields, List<String> cols, List<String> operators, List<String> values, String  fromDomain, String reportPeriod){
		Session session = sessionFactory.getCurrentSession();
		String queryStatement = "select ";
		Integer i=0;
		for (String field : fields) {
			if(field == fields.get(fields.size() -1))
				queryStatement += field + " from " + fromDomain + " where (";
			else
				queryStatement += field + ", ";
		}

		for (String col : cols) {
			
			if(i<cols.size() - 1)
				queryStatement +=  col + " " + operators.get(i) + " '" + values.get(i) + "' OR ";
			else
				queryStatement +=  col + " " + operators.get(i) + " '" + values.get(i) + "')";
			i++;
		}

		java.util.Date startDate = new java.util.Date();
		java.util.Date endDate = new java.util.Date();
		if(reportPeriod == null || reportPeriod.equalsIgnoreCase("")){
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		java.util.Date date1 = new java.util.Date();
		Calendar endCalendar;
		endCalendar = Calendar.getInstance();
		endCalendar.setTime(date1);
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(date1);
		if(reportPeriod=="daily"){
			startCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.get(Calendar.DAY_OF_MONTH)-1);
			int daily = startCalendar.get(Calendar.DAY_OF_MONTH);
		}
		else if(reportPeriod=="weekly"){
			startCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.get(Calendar.DAY_OF_MONTH)-7);
			int daily = startCalendar.get(Calendar.DAY_OF_MONTH);
		}
		else if(reportPeriod=="monthly"){
			startCalendar.set(Calendar.MONTH, endCalendar.get(Calendar.MONTH)-1);
			int monthly = startCalendar.get(Calendar.MONTH);
		}
		else if(reportPeriod=="yearly"){
			startCalendar.set(Calendar.YEAR, endCalendar.get(Calendar.YEAR)-1);
			int monthly = startCalendar.get(Calendar.YEAR);
		}
		startDate = startCalendar.getTime();
		endDate = endCalendar.getTime();
		}
		//queryStatement += "and ( leadOwner.id )";
		//queryStatement = "from Lead as Lead where Lead.id = 1";
		//queryStatement = "select Lead.fName, Lead.lName from Lead as Lead where Lead.id = 1";
//		String createdDate = "select t.dateCreated form " + fromDomain + " t";
//		String modifiedDate = "select t.dateModified form " + fromDomain + " t";
//		Query createdDateQuery = session.createQuery(createdDate);
//		Query modifiedDateQuery = session.createQuery(modifiedDate);
		queryStatement += "dateCreated btween ";
		Query query = session.createQuery(queryStatement);
//		query.setParameter("startDate", startDate);
//		query.setParameter("endDate", endDate);
		List<String> l =query.list();
        @SuppressWarnings("rawtypes")
		Iterator it = l.iterator();
        
        while(it.hasNext())
        {
            Object o[] = (Object[])it.next();
        }
		return l;
	}

}
